#!/usr/bin/env python
#
# Common core constants and utility routines that may be used in different
# modules, or for communication between modules. These items have been
# separated from their major implementation modules to avoid circular imports
# when multiple modules share the same constants.
#
# Author:   Mike Murphy <mamurph@cs.clemson.edu>
# Revision: 23 September 2009
#
#   Copyright 2009 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

'''
Common constants and utility functions used throughout the simulator.
'''

import math

# Scheduler machine states
SC_CLAIMED = 0
SC_FREE = 1
SC_SHUTDOWN = 2

# Scheduler metric fields
SM_WAITING = 0
SM_RUNNING = 1
SM_COMPLETED = 2
SM_ERRORS = 3
SM_TOTAL = 4
SM_FREE = 5

# Pilot job status signal numbers
PJ_START = 0
PJ_STOP = 15
PJ_KILL = 9


def generate_name(basename, index, count, start = 1):
   '''
   Generates names of the form BASE1, BASE01, BASE001, etc. These names are
   useful for providing unique names to machines or other indexed items.

   @param basename: Base name to which the suffix will be appended
   @type basename: str
   @param index: Index of the current item
   @type index: int
   @param count: Total number of items, for automatic left padding with zeroes
   @type count: int
   @param start: Starting item number (default 1)
   @type start: int

   @rtype: str
   @return: formatted name
   '''
   fmt = '%s%%0%dd' % (basename, int(math.log10(start + count)) + 1)
   return fmt % index
#
